﻿using WRJClient.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WRJClient.Common;
using Newtonsoft.Json;
using WRJClient.DAL;

namespace WRJClient.Net
{
    public class MissionMessageProcessor : IMessageProcessor
    {
        private bool parseMission(string jsonData, out MissionModel missionModel)
        {
            missionModel = null;

            try
            {
                var missionData = JsonConvert.DeserializeObject<dynamic>(jsonData);
                missionModel = new MissionModel
                {
                    DbId = missionData.dbId,
                    Name = missionData.name,
                    CreateTime = missionData.createTime,
                    SendTime = missionData.sendTime,
                    StartTime = missionData.startTime,
                    EndTime = missionData.endTime,
                    //UavId
                    //WorkStationId
                    Type = missionData.type,
                    Details = missionData.details,
                    //AirlineId
                    Report = missionData.report,
                    State = missionData.state
                };
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
            return true;
        }

        public void Process(TopicData topicData)
        {
            switch (topicData.eventId)
            {
                case 1://任务新建事件
                    MissionAddProcess(topicData);
                    break;
                case 2://任务删除事件
                    MissionDelProcess(topicData);
                    break;
                case 3://任务开始事件
                    MissionBeginProcess(topicData);
                    break;
                case 4://任务结束事件
                    MissionEndProcess(topicData);
                    break;
                case 5://任务下发事件
                    MissionPublishProcess(topicData);
                    break;
                case 6://任务状态事件
                    MissionStateChangedProcess(topicData);
                    break;
                case 7://任务绑定航线事件
                    MissionBindingAirlineProcess(topicData);
                    break;
                case 8://任务完成事件
                    MissionFinishedProcess(topicData);
                    break;
                case 9://任务修改事件
                    MissionModifiedProcess(topicData);
                    break;
                case 10://任务装载事件
                    MissionLoadedProcess(topicData);
                    break;
                default:
                    break;
            }
        }

        private void MissionAddProcess(TopicData topicData)
        {
            MissionModel model = null;
            if (parseMission(topicData.data, out model))
            {
                DataLoader.Instance.LoadMission();//UpdateManager.Instance.Count();

                MessageManager.Instance.Info(string.Format("新建任务[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("新建任务[{0}]!", topicData.id), topicData.data);
            }
        }
        private void MissionDelProcess(TopicData topicData)
        {
            MissionModel model = null;
            if (parseMission(topicData.data, out model))
            {
                DataLoader.Instance.LoadMission();//UpdateManager.Instance.Count();
                MessageManager.Instance.Info(string.Format("删除任务[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("删除任务[{0}]!", topicData.id), topicData.data);
            }
        }
        private void MissionBeginProcess(TopicData topicData)
        {
            MissionModel model = null;
            if (parseMission(topicData.data, out model))
            {
                MissionManager.Instance.UpdateMission(topicData.id, model);
                MessageManager.Instance.Info(string.Format("任务开始[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("任务开始[{0}]!", topicData.id), topicData.data);
            }
        }
        private void MissionEndProcess(TopicData topicData)
        {
            MissionModel model = null;
            if (parseMission(topicData.data, out model))
            {
                MissionManager.Instance.UpdateMission(topicData.id, model);
                MessageManager.Instance.Info(string.Format("任务结束[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("任务结束[{0}]!", topicData.id), topicData.data);
            }
        }
        private void MissionPublishProcess(TopicData topicData)
        {
            MissionModel model = null;
            if (parseMission(topicData.data, out model))
            {
                MissionManager.Instance.UpdateMission(topicData.id, model);
                MessageManager.Instance.Info(string.Format("任务下发[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("任务下发[{0}]!", topicData.id), topicData.data);
            }
        }
        private void MissionStateChangedProcess(TopicData topicData)
        {
            MissionModel model = null;
            if (parseMission(topicData.data, out model))
            {
                if (model.State == 2)
                {
                    MissionManager.Instance.UpdateMission(topicData.id, model);
                    MessageManager.Instance.Info(string.Format("任务发布[{0}]!", model.Name));
                }
            }
            else
            {
                MessageManager.Instance.Error(string.Format("任务状态[{0}]!", topicData.id), topicData.data);
            }
        }
        private void MissionBindingAirlineProcess(TopicData topicData)
        {
            MissionModel model = null;
            if (parseMission(topicData.data, out model))
            {
                MissionManager.Instance.UpdateMission(topicData.id, model);
                MessageManager.Instance.Info(string.Format("绑定航线[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("绑定航线[{0}]!", topicData.id), topicData.data);
            }
        }
        private void MissionFinishedProcess(TopicData topicData)
        {
            MissionModel model = null;
            if (parseMission(topicData.data, out model))
            {
                MissionManager.Instance.UpdateMission(topicData.id, model);
                MessageManager.Instance.Info(string.Format("任务完成[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("任务完成[{0}]!", topicData.id), topicData.data);
            }
        }
        private void MissionModifiedProcess(TopicData topicData)
        {
            MissionModel model = null;
            if (parseMission(topicData.data, out model))
            {
                MissionManager.Instance.UpdateMission(topicData.id, model);
                MessageManager.Instance.Info(string.Format("任务修改[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("任务修改[{0}]!", topicData.id), topicData.data);
            }
        }
        private void MissionLoadedProcess(TopicData topicData)
        {
            MissionModel model = null;
            if (parseMission(topicData.data, out model))
            {
                MissionManager.Instance.UpdateMission(topicData.id, model);
                MessageManager.Instance.Info(string.Format("任务装载[{0}]!", model.Name));
            }
            else
            {
                MessageManager.Instance.Error(string.Format("任务装载[{0}]!", topicData.id), topicData.data);
            }
        }
    }
}
